package leetcode;

/**
 * 描述:
 *
 * @Package: leetcode
 * @ClassName: 搜索插入位置
 * @Author: 陈威
 * @CreateTime: 2022/1/26 13:34
 * @Description:
 */
public class 搜索插入位置 {
    public int searchInsert(int[] nums, int target) {
        if(target>nums[nums.length-1]){
            return nums.length;
        }
        int left=0,right=nums.length-1;
        int temp=0;
        while (left<=right){
            int mid=(left+right)/2;
            if(nums[mid]==target){
                return mid;
            }else if(nums[mid]>target){
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        return left;
    }

    public static void main(String[] args) {
        int []nums= {1,3,5,6};
        搜索插入位置 sea=new 搜索插入位置();
        System.out.println(sea.searchInsert(nums, 2));
    }
}
